﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP.Group
{
    /// <summary>CCPSIP_019：離開聊天群組 API</summary>
    /// idHeader = 裝置 UUID 前八碼。iOS 請使用 [CCPCrypto getIdHeader] 呼叫。
    /// checkcode = 裝置限時驗證碼。iOS 請使用 [CCPCrypto getCheckcode] 呼叫。
    /// GroupID = Client端的群組ID。
    /// Leaver = 離開的成員帳號。(目前只有本人能離開，未來會加入群組管理員功能)

    public class leave_group : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            try
            {
                //【01】取得POST參數，驗證身分
                //
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];
                string ClientGID = req.Form["GroupID"];//Client端的群組ID
                string Leaver = req.Form["Leaver"];//離開的成員帳號
                //string NewHost = req.Form["NewHost"];//新的群組Host(如果Host離開)


                SipUserInfo userInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (userInfo == null)
                {
                    throw new Exception("Error_01: Cannot identify this device");//無法識別此裝置
                }


                if (userInfo.SIP_Num != Leaver)
                {
                    //【02-1】檢查Leaver是否為群組成員
                    //
                    if (!OracleKitGroup.isMemberOfGroup(Leaver, ClientGID))
                    {
                        throw new Exception("Error_02: User is not a member");//離開者不是群組成員
                    }

                    //【02-2】檢查User是否為Host
                    //
                    if (!OracleKitGroup.isHostOfGroup(userInfo.SIP_Num, ClientGID))
                    {
                        throw new Exception("Error_03: You cannot do this action");//無權限執行
                    }
                }


                //【03】DB Operation: 離開聊天群組
                //
                if (OracleKitGroup.isMemberOfGroup(Leaver, ClientGID))
                {
                    OracleKitGroup.LeaveGroup(userInfo, ClientGID, Leaver);
                }


                //【98】通知Push Service
                //
                PushKit.WakePushAsync(PushKit.PushType.Msg);


                //【99】回傳結果
                //
                resultStr = "OK";
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
